<?php

class m120428_150214_create_user extends CDbMigration {

	public function safeUp() {
		$user = array(
			'id'=>'pk',
			'username'=>'varchar(20) not null',
			'password'=>'varchar(128) not null',
			'email'=>'varchar(128) not null',
			'activkey'=>"varchar(128) not null default ''",
			'createtime'=>"integer(10) not null default '0'",
			'lastvisit'=>"integer(10) not null default '0'",
			'superuser'=>"integer(1) not null default '0'",
			'status'=>"integer(1) not null default '0'",
		);
		$this->createTable('users', $user);

		$user = array(
			'id'=>1,
			'username'=>'admin',
			'password'=>'21232f297a57a5a743894a0e4a801fc3',
			'email'=>'hayate@ashitahadochida.site11.com',
			'activkey'=>'21232f297a57a5a743894a0e4a801fc3',
			'createtime'=>'1261146094',
			'lastvisit'=>0,
			'superuser'=>1,
			'status'=>1,
		);
		$this->insert('users', $user);

		$profile = array(
			'user_id'=>'pk',
			'lastname'=>"varchar(50) not null default ''",
			'firstname'=>"varchar(50) not null default ''",
			'birthday'=>"date not null default '0000-00-00'",
		);
		$this->createTable('profiles', $profile);

		$profile = array(
			'user_id'=>1,
			'lastname'=>'Admin',
			'firstname'=>'Hayate',
			'birthday'=>'0000-00-00',
		);
		$this->insert('profiles', $profile);

		$profileField = array(
			'id'=>'pk',
			'varname'=>"varchar(50) not null",
			'title'=>"string not null",
			'field_type'=>"varchar(50) not null",
			'field_size'=>"integer(3) not null default '0'",
			'field_size_min'=>"integer(3) not null default '0'",
			'required'=>"integer(1) not null default '0'",
			'match'=>"string not null default ''",
			'range'=>"string not null default ''",
			'error_message'=>"string not null default ''",
			'other_validator'=>"text not null default ''",
			'default'=>"string not null default ''",
			'widget'=>"string not null default ''",
			'widgetparams'=>"text not null default ''",
			'position'=>"integer(3) not null default '0'",
			'visible'=>"integer(1) not null default '0'",
		);
		$this->createTable('profile_fields', $profileField);

		$profileFields = array(
			array(
				'id'=>1,
				'varname'=>'lastname',
				'title'=>'Last Name',
				'field_type'=>'VARCHAR',
				'field_size'=>50,
				'field_size_min'=>3,
				'required'=>1,
				'match'=>'',
				'range'=>'',
				'error_message'=>'Incorrect Last Name (length between 3 ' .
				'and 50 characters).',
				'other_validator'=>'',
				'default'=>'',
				'widget'=>'',
				'widgetparams'=>'',
				'position'=>1,
				'visible'=>3
			),
			array(
				'id'=>2,
				'varname'=>'firstname',
				'title'=>'First Name',
				'field_type'=>'VARCHAR',
				'field_size'=>50,
				'field_size_min'=>3,
				'required'=>1,
				'match'=>'',
				'range'=>'',
				'error_message'=>'Incorrect First Name (length between 3 ' .
				'and 50 characters).',
				'other_validator'=>'',
				'default'=>'',
				'widget'=>'',
				'widgetparams'=>'',
				'position'=>1,
				'visible'=>3
			),
			array(
				'id'=>3,
				'varname'=>'birthday',
				'title'=>'Birthday',
				'field_type'=>'DATE',
				'field_size'=>0,
				'field_size_min'=>0,
				'required'=>2,
				'match'=>'',
				'range'=>'',
				'error_message'=>'Incorrect Last Name (length between 3 ' .
				'and 50 characters).',
				'other_validator'=>'',
				'default'=>'0000-00-00',
				'widget'=>'UWjuidate',
				'widgetparams'=>'{\"ui-theme\":\"redmond\"}',
				'position'=>3,
				'visible'=>2
			),
		);
		foreach ($profileFields as $profileField) {
			$this->insert('profile_fields', $profileField);
		}

		$auth = Yii::app()->authManager;
		$auth->createRole('admin', 'Administrator');
		$auth->assign('admin', 1);
	}

	public function safeDown() {
		$this->dropTable('users');
		$this->dropTable('profiles');
		$this->dropTable('profiles_fields');
	}

}